<?php

namespace common\service;

use common\service\GlobalUrlService;
use Yii;

class StaticService {

    public static function includeAppStatic($type, $path, $depend)
    {
        $release_version = defined("RELEASE_VERSION") ? RELEASE_VERSION : time();
        $path = $path."?ver={$release_version}";
        if($type=="css")
        {
            Yii::$app->getView()->registerCssFile($path,['depends'=>$depend]);
        }else{
            Yii::$app->getView()->registerJsFile($path,['depends'=>$depend]);
        }
    }

    /**
     * 引入静态的js文件
     * @param $path
     * @param $depend
     */
    public static function includeAppJsStatic($path,$depend)
    {
        self::includeAppStatic("js",$path,$depend);
    }

    /**
     * 引入静态的css文件.
     * @param $path
     * @param $depend
     */
    public static function includeAppCssStatic($path,$depend)
    {
        self::includeAppStatic("css",$path,$depend);
    }

    /**
     * 加载静态资源
     * @param $type
     * @param $path
     * @param $depend
     */
    public static function includeStatic($type, $path, $depend)
    {
        $release_version = defined("RELEASE_VERSION") ? RELEASE_VERSION : time();
        if (stripos($path, "?") !== false)
        {
            $path = $path . "&ver={$release_version}";
        } else {
            $path = $path . "?ver={$release_version}";
        }
        if ($type == "css") {
            Yii::$app->getView()->registerCssFile(GlobalUrlService::buildStaticUrl($path), ['depends' => $depend]);
        } else {
            Yii::$app->getView()->registerJsFile(GlobalUrlService::buildStaticUrl($path), ['depends' => $depend]);
        }
    }

    public static function includeStaticJs($path, $depend)
    {
        self::includeStatic("js", $path, $depend);
    }
    public static function includeStaticCss($path, $depend)
    {
        self::includeStatic("css", $path, $depend);
    }
}
